package org.icec.web.sys.service;

import org.beetl.sql.core.engine.PageQuery;
import org.icec.web.sys.dao.SysClientDao;
import org.icec.web.sys.dao.SysClientYxDao;
import org.icec.web.sys.dao.TimerDayDao;
import org.icec.web.sys.model.SysClient;
import org.icec.web.sys.model.SysClientYx;
import org.icec.web.sys.model.SysUser;
import org.icec.web.sys.model.TimerDay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

/*
* 
* gen by icec  2017-11-01
*/
@Service
public class SysClientService {
	@Autowired
	private SysClientDao sysClientDao ;


	@Autowired
	private TimerDayDao timerDayDao ;

	@Autowired
	private SysClientYxDao sysClientYxDao;
	/**
	*
	*保存
	*/
	@Transactional
	public void save(SysClient sysClient, SysUser user,String uploadpath){
		Date date=new Date();
		TimerDay timerDay=new TimerDay();
		sysClient.setInsertdate(date);
		sysClient.setDelFlag(0);
		sysClient.setIsclientgxc("否");
		sysClient.setIsclientgxc("是");
		sysClient.setUserid(user.getId());
		String clientno=user.getNo()+"-" +getyy_mm_dd(date)+"-"+getSexNum(sysClient.getClientsex()) +"-"+getmaxid();//复制给客户编码
		sysClient.setClientno(clientno);
//		sysClient.setFilepath(copyFile(uploadpath,clientno,sysClient.getClientsex(),getyy_mm(date)));
		timerDayDao.insert(timerDay);//客户号自增
		sysClientDao.insert(sysClient);//增加客户
		SysClientYx sysClientYx=new SysClientYx();
		sysClientYx.setClientid(clientno);
		sysClientYxDao.insert(sysClientYx);//增加个人信息表id--clientid

	}

	/**
	 * 复制文件到相应文件夹
	 * @param uploadpath
	 * @param clientno
	 * @param sex
	 * @param date
	 * @return
	 */
	public  final String   copyFile(String uploadpath,String clientno,String sex,String date){
		String oldpath;
		if (sex.equals("男")){
			 oldpath=uploadpath+"/work/man.pptx";
		}else  if (sex.equals("女")){
			oldpath=uploadpath+"/work/woman.pptx";
		}else{
			oldpath=uploadpath+"/work/weizhi.pptx";
		}
		//原始路径

		//创建目录
		String dirName = uploadpath+"/work/sm/client/"+date+"/"+clientno;
		CreateFileUtil.createDir(dirName);
		//新路径
		String newpath=uploadpath+"/work/sm/client/"+date+"/"+clientno+"/"+clientno+".pptx";
		String newpath1=uploadpath+"/work/sm/client/"+date+"/"+clientno;
		CreateFileUtil.copyFile(oldpath,newpath);
		return newpath1;
	}

	/**
	 * 获取最大id
	 * @return
	 */
	public  final   String getmaxid(){
		String Sa=timerDayDao.findmaxid().toString();
		if (Sa.length()==1){
			Sa="000"+Sa;
		}else if (Sa.length()==2) {
			Sa = "00" + Sa;
		}else if (Sa.length()==3) {
			Sa = "0" + Sa;
		}
		return Sa;
	}

	/**
	 * 获取时间节点
	 * @param date
	 * @return
	 */
	public final   String getyy_mm_dd(Date date){
		SimpleDateFormat year = new SimpleDateFormat("yy");
		SimpleDateFormat mon = new SimpleDateFormat("MM");
		SimpleDateFormat day = new SimpleDateFormat("dd");
		String formatDate1 = year.format(date);
		String formatDate2 = mon.format(date);
		String formatDate3 = day.format(date);
		String formatDate=formatDate1+formatDate2+formatDate3;
		return formatDate;
	}

	public final   String getyy_mm(Date date){
		SimpleDateFormat year = new SimpleDateFormat("yy");
		SimpleDateFormat mon = new SimpleDateFormat("MM");
		String formatDate1 = year.format(date);
		String formatDate2 = mon.format(date);
		String formatDate=formatDate1+formatDate2;
		return formatDate;
	}

	/**
	 * 获取男女数字
	 * @param sex
	 * @return
	 */
	public final   Integer getSexNum(String sex){
		if (sex.equals("男")){
			return 1;
		}else if (sex.equals("女")){
			return 2;
		}
		return 0;
	}


	/**
	 * 客户分页查询
	 * @param query
	 * @return
	 */
	public PageQuery<SysClient> queryClient(PageQuery<SysClient> query, SysUser user) {

		return sysClientDao.pageQuery(query,user.getId());
	}


	/**
	 * 客户分页查询不需要登录信息
	 * @param query
	 * @return
	 */
	public PageQuery<SysClient> queryClientGx(PageQuery<SysClient> query) {
		return sysClientDao.pageQueryGx(query);
	}

	/**
	 * 根据主键id查询
	 *
	 * @param id
	 * @return
	 */
	public SysClient findById(Integer id) {

		return sysClientDao.findById(id);
	}


	/**
	 * 名片资料查询
	 *
	 * @param id
	 * @return
	 */
	public SysClient findallById(Integer id) {

		return sysClientDao.findallById(id);
	}


	/**
	 * 删除操作，更新del_flag字段
	 *
	 * @param ids
	 */
	@Transactional
	public void deleteAll(String ids) {
		String[] idarr = ids.split(",");
		for (String id : idarr) {
			SysClient sysClient = new SysClient();
			sysClient.setId(Integer.parseInt(id));
			sysClient.setDelFlag(1);
			sysClientDao.updateTemplateById(sysClient);
		}

	}


	/**
	 * 更新操作，更新del_flag字段
	 *
	 * @param
	 */
	@Transactional
	public void update(SysClient sysClient, SysUser sysUser) {
		    sysClient.setUpdateuserid(sysUser.getId());
		    sysClient.setUpdatetime(new Date());
			sysClientDao.updateTemplateById(sysClient);
		}



	}



